#!/usr/bin/env python
#coding=utf-8

import time
from flask import request, session, current_app, flash, make_response

from codingabc.database import db
from app.services.xsl import XLSWriter, XlsDoc
from codingabc.helpers import toint, log_debug, log_info, log_error, log_debug, randomstr, ismobile, format_timestamp, get_count


class AccountService(object):
    """account service"""
    def __init__(self, uid=0):
        self.errmsg       = {}

    def df_list_export(self, export_list):
        """ 导出okex币币帐户列表"""

        xlswriter  = XLSWriter()
        sheet_name = u'币种账户列表'
        export_title = [u'币种', u'冻结', u"借贷", u'可用']

        xlswriter.writerow(export_title, sheet_name=sheet_name)

        for e in export_list:
            export_data = [e[0],
                           e[1],
                           e[2],
                           e[3]]
            xlswriter.writerow(export_data, sheet_name=sheet_name)
        s = xlswriter.wbk.get_biff_data()
        doc = XlsDoc()
        s = doc.savestream(s)
        response = make_response(s)
        response.headers['Content-Type'] = 'application/vnd.ms-excel'
        response.headers['Content-Disposition'] = 'attachment; filename=币种账户列表.xls'

        return response

    def df1_list_export(self, export_list, sum_account):
        """ 导出okex币种折合usdt列表"""

        xlswriter  = XLSWriter()
        sheet_name = u'币种折合usdt列表'
        export_title = [u'币种', u'可用+冻结', u"价格", u'折合usdt']

        xlswriter.writerow(export_title, sheet_name=sheet_name)

        for e in export_list:
            export_data = [e[0],
                           e[1],
                           e[2],
                           e[3]]
            xlswriter.writerow(export_data, sheet_name=sheet_name)

        xlswriter.writerow([], sheet_name=sheet_name)  # 写一行空的
        xlswriter.writerow(['', '', '', u'合计:%s'% sum_account], sheet_name=sheet_name)

        s = xlswriter.wbk.get_biff_data()
        doc = XlsDoc()
        s = doc.savestream(s)
        response = make_response(s)
        response.headers['Content-Type'] = 'application/vnd.ms-excel'
        response.headers['Content-Disposition'] = 'attachment; filename=币种折合usdt列表.xls'

        return response

    def a_list_export(self, export_list):
        """ 导出火币账户列表"""

        xlswriter  = XLSWriter()
        sheet_name = u'火币账户列表'
        export_title = [u'币种', u'交易所', u"导出时间", u'持仓数量', u'折合usdt']

        xlswriter.writerow(export_title, sheet_name=sheet_name)

        for e in export_list:
            export_data = [e[0],
                           e[1],
                           e[2],
                           e[3],
                           e[4]]
            xlswriter.writerow(export_data, sheet_name=sheet_name)

        s = xlswriter.wbk.get_biff_data()
        doc = XlsDoc()
        s = doc.savestream(s)
        response = make_response(s)
        response.headers['Content-Type'] = 'application/vnd.ms-excel'
        response.headers['Content-Disposition'] = 'attachment; filename=火币账户列表.xls'

        return response


    def ba_list_export(self, export_list):
        """ 导出币安账户列表"""

        xlswriter  = XLSWriter()
        sheet_name = u'币安账户列表'
        export_title = [u'币种', u'交易所', u"导出时间", u'持仓数量', u'折合usdt']

        xlswriter.writerow(export_title, sheet_name=sheet_name)

        for e in export_list:
            export_data = [e[0],
                           e[1],
                           e[2],
                           e[3],
                           e[4]]
            xlswriter.writerow(export_data, sheet_name=sheet_name)

        s = xlswriter.wbk.get_biff_data()
        doc = XlsDoc()
        s = doc.savestream(s)
        response = make_response(s)
        response.headers['Content-Type'] = 'application/vnd.ms-excel'
        response.headers['Content-Disposition'] = 'attachment; filename=币安账户列表.xls'

        return response
